package main

import (
	"UpdateTools/model"
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"
)

/*
@Project        :   UpdateTools
@FileName       :   HandleFile.go
@Author         :   Administrator
@Datetime       :   2020/9/13 1:06
@Software       :   GoLand
@Show           :
*/

func sumCMD5(file string) string {
	f, _ := os.Open(file)
	md5hash := md5.New()
	if _, err := io.Copy(md5hash, f); err != nil {
		fmt.Println("Copy", err)
		return ""
	}
	_ = f.Close()
	md5hash.Sum(nil)
	return hex.EncodeToString(md5hash.Sum(nil))
}

func HandelFile(filePath string, localFile LocalFile, serviceName string) string {
	fileMd5 := sumCMD5(filePath)
	md5Result := model.ThanMd5(fileMd5, localFile.Path, serviceName)
	if md5Result == "0" {
		lastver := model.CreateUpdate(model.LocalFile(localFile), serviceName, fileMd5)
		return lastver
	} else {
		return md5Result
	}
}
